/* ***********************
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is BriefShowUntil.js module.
 *
 * The Initial Developer of the Original Code is Rob Gerns.
 *
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 **************************/


/*************************************************
* Function Name(s): briefShowUntil(), briefCheckShowUntil()
*
* Purpose:
* Check to see if the Oister file has a showuntil 
* datetime defined. If so, check to see if the datetime
* has happened and hide the entire Oister section of the HTML 
* if it has.
***************************************************/  

function briefShowUntil(xmlDocument){


 //Count and retrieve showuntil datetime, if it exists
 var briefShowUntilNode = xmlDocument.getElementsByTagName("showuntil");
  
 if(briefShowUntilNode != undefined && briefShowUntilNode != null){
  if(briefShowUntilNode.length > 0) {
   var briefShowUntilPre = briefShowUntilNode[0].firstChild.nodeValue;
  }
	 //Reformat the datetime so that there are no dashes, letters, or colons, remove any timezone info since we're going by the user's system time
   if(briefShowUntilPre != undefined && briefShowUntilPre != null){  
   
    var replaceTimeZone = briefShowUntilPre.replace(/\D\d\D\d\d/gi, "");
    var xmlShowUntilDateRFMTPre = replaceTimeZone.replace(/\D/gi, "");
   
    //convert the datetime to a string
    xmlShowUntilDateRFMTPre.toString();
    
    //remove the last four digits (timezone)
    var xmlShowUntilDateRFMT = xmlShowUntilDateRFMTPre.substring(0, xmlShowUntilDateRFMTPre.length - 4);
 
   
/*********************************************
* Now we get the current date from the user's system clock and put it all
* into one big string.  The format of the datetime will then match the format
* that was created for the showuntil datetime.
*********************************************/

    var jsDT = new Date();
    var realMonth = jsDT.getMonth() + 1;
 
    if(realMonth < 10) {
    realMonth = "0" + realMonth;
    }
 
    var minutes = jsDT.getMinutes();
    var seconds = jsDT.getSeconds();
    var hours = jsDT.getHours();

    if(hours < 10) {
     hours = "0" + hours;
    }
    
    if(minutes < 10) {
     minutes = "0" + minutes;
    }
    
    if(seconds < 10) {
     seconds = "0" + seconds;
    }

		//Now to put it all into one big string
    var jsFullDateTime = jsDT.getFullYear() + '' + realMonth + '' + jsDT.getDate() + '' + hours + '' + minutes + '' + seconds;
		
		//call the function that will compare the system datetime with the showuntil datetime
		checkShowUntil(jsFullDateTime,xmlShowUntilDateRFMT);

   }

 }

}

function checkShowUntil(jsFullDateTime,xmlShowUntilDateRFMT){

/*******************************************
*If the showuntil time is equal or less than the
*user's system time, set every dom node in the feature
*to have a style.display value of 'none'
********************************************/

 //first convert the user's current system datetime to an integer (remember we made it a string in the previous function
 var parsedJSFDT = parseInt(jsFullDateTime);
	
 //convert the showuntil datetime to an integer (remember we made it a string in the previous function)
 var parsedSUDR = parseInt(xmlShowUntilDateRFMT);

 //check to see if the user's system time is equal or greater than the showuntil datetime and if so, set the "container" div style display to none 
 if(parsedJSFDT >= parsedSUDR) {
  document.getElementById("container").style.display="none";   
 }

}

